<?php
namespace app\common\service;


use think\Db;
use think\Model;

class CounselorAreaClassService extends BaseService
{
    /**
     * 添加区域
     * @param $sid
     * @param $postParam
     * @return array
     */
    public function AddArea($sid,$postParam){
        //父类区域信息
        if($postParam['pid'] == 0){
            $path = 0;
        }else{
            $parent_info = db('counselor_area_class')->where(['ac_id' => $postParam['pid']])->field('path')->find();
            $path = $parent_info['path'].'-'.$postParam['pid'];
        }
        $add_data = [
            'ac_name' => $postParam['ac_name'],
            'pid' => $postParam['pid'],
            'sid' => $sid,
            'leader_uid' => $postParam['leader_uid'],
            'addtime' => time(),
            'path' => $path,
            'status' => 1
        ];
        $insert_id = db('counselor_area_class')->insert($add_data,false,true);
        //设置该顾问为管理员
        $where = [
            'sid' => $sid,
            'userid' =>$postParam['leader_uid']
        ];
        $res = db('user_integral')
                ->where($where)
                ->update(['counselor_ac_path' => $path.'-'.$insert_id.'-','counselor_ac_id'=>$insert_id,'is_ac_admin'=>1]);

        if($res === false){
            return ['status' => 0];
        }else{
            return ['status' => 1];
        }
    }

    /**
     * 编辑区域
     * @param $sid
     * @param $id
     * @param $postParam
     * @return array
     * @throws \think\Exception
     */
    public function EditArea($sid,$id,$postParam){
        //父类区域信息
        if($postParam['pid'] == 0){
            $path = 0;
        }else{
            $ppath = db('counselor_area_class')->where(['ac_id' => $postParam['pid']])->value('path');
            $path = $ppath.'-'.$postParam['pid'];
        }
        $where = [
            'sid' => $sid,
            'ac_id' => $id
        ];
        $update_data = [
            'ac_name' => $postParam['ac_name'],
            'pid' => $postParam['pid'],
            'leader_uid' => $postParam['leader_uid'],
            'path' => $path
        ];
        $res = db('counselor_area_class')->where($where)->update($update_data);
        if($res !==false){
            //修改区域管理员
            $where = [
                'sid' => $sid,
                'userid' =>$postParam['leader_uid']
            ];
            //之前该区域的管理员清理
            $del_where = [
                'sid' => $sid,
                'counselor_ac_id' => $id
            ];
            db('user_integral')
                ->where($del_where)
                ->update(['is_ac_admin'=>0]);
            $res = db('user_integral')
                ->where($where)
                ->update(['counselor_ac_path' => $path.'-'.$id.'-','counselor_ac_id'=>$id,'is_ac_admin'=>1]);

            if($res === false){
                return ['status' => 0];
            }else{
                return ['status' => 1];
            }
        }else{
            return ['status' => 0];
        }

    }

    /**
     * 删除区域
     * @param $sid
     * @param $id
     * @return array
     * @throws \think\Exception
     */
    public function DelArea($sid,$id){
        //没有顾问的区域才能删除
        $counselor_info = db('user_integral')->where(['counselor_status'=>1,'sid' => $sid])->whereLike('counselor_ac_id','%-'.$id.'-%')->field('userid')->select();
        if(empty($counselor_info)){
            $res = db('counselor_area_class')->where(['ac_id' => $id])->delete();
            if($res === false){
                return ['status' => 0,'msg' => '删除失败'];
            }else{
                return ['status' => 1];
            }
        }else{
            return ['status' => 0,'msg' => '该区域下还有顾问，请先转移顾问，再删除！'];
        }
    }

    /**
     * 获取区域列表
     * @param $sid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function GetAreaList($sid){
        $where = [
            'sid' => $sid,
            'status' => 1
        ];
        $list_count = db('counselor_area_class')->where($where)->count();
        $list = db('counselor_area_class')->where($where)->field('*,concat(path,\'-\',ac_id) as bpath')->order("bpath ASC")->select();
        foreach($list as $key=>$value){
            $list[$key]['count']=count(explode('-',$value['path']));
            if($list[$key]['count']>0)
            {
                $list[$key]['treeName'] = $this->getStr($list[$key]['count']).$list[$key]['ac_name'];
            }
            else
            {
                $list[$key]['treeName'] = $list[$key]['ac_name'];
            }
            //商务人数
            $list[$key]['counselor_num'] = $this->getCounselorNumByAcID($sid,$value['ac_id']);
        }
        return ['content' => $list,'page' => ['count' => $list_count]];
    }

    private function getStr($p)
    {
        $str = "   ";
        for($i=1;$i<$p;$i++)
        {
            $str.="       ";
        }
        return $str;
    }

    /**
     * 根据区域分类查找该区域下的商务总人数
     * @param $sid
     * @param $ac_id
     * @return int|string
     */
    private function getCounselorNumByAcID($sid,$ac_id){
        $where = [
            'sid' => $sid,
            'counselor_status'=>1
        ];
        return db('user_integral')->where($where)->whereLike('counselor_ac_path',['%-'.$ac_id.'-%'])->count();
    }
    /**
     * 获取区域详情
     * @param $sid
     * @param $id
     * @return array
     */
    public function GetAreaDetail($sid,$id){
        $where = [
            'sid' => $sid,
            'ac_id' => $id
        ];
        $res = db('counselor_area_class')->where($where)->find();
        if(empty($res)){
            return ['status' => 0,'msg' => '参数错误'];
        }else{
            $data['content'] = $res;
            return ['status' => 1,'data' => $data];
        }
    }
    /**
     * 获取区域名称
     * @param $sid
     * @param $id
     * @return array
     */
    public function GetAreaName($sid,$id){
        $where = [
            'sid' => $sid,
            'ac_id' => $id
        ];
        $res = db('counselor_area_class')->where($where)->find();
        return $res['ac_name'];
    }
}